<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from '../../../../dashboard/mixins/resize'

export default {
  name: 'LineChart',
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '100%'
    },
    autoResize: {
      type: Boolean,
      default: true
    },
    chartData: {
      type: Object,
      required: false
    }
  },
  data() {
    return {
      chart: null
    }
  },
  // watch: {
  //   chartData: {
  //     deep: true,
  //     handler(val) {
  //       this.setOptions(val)
  //     }
  //   }
  // },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      this.chart.setOption({
        title: {
          text: '月销售趋势分析',
          textStyle: {
            color: '#333',
            fontWeight: 'bolder',
            fontSize: 18,
          }
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross',
            label: {
              backgroundColor: '#6a7985'
            }
          }
        },
        legend: {
          data: ['订单金额', '订单数量']
        },
        grid: {
          left: '3%',
          right: '4%',
          bottom: '10%',
          containLabel: true
        },
        dataZoom: [
          {
            show: true,
            realtime: true,
            start: 20,
            end: 70,
          },
          {
            type: 'inside',
            realtime: true,
            start: 20,
            end: 70,
          }
        ],
        xAxis: [
          {
            type: 'category',
            boundaryGap: false,
            data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
          }
        ],
        yAxis: [
          {
            type: 'value'
          }
        ],
        series: [
          {
            name: '订单金额',
            type: 'line',
            stack: null,
            smooth: true,  // 设置为圆滑线
            areaStyle: {
              color: 'rgba(57, 207, 160, 0.5)', 
            },
            emphasis: {
              focus: 'series'
            },
            data: [120, 132, 101, 134, 90, 230, 210]
          },
          {
            name: '订单数量',
            type: 'line',
            stack: null,
            smooth: true,  // 设置为圆滑线
            areaStyle: {
              color: 'rgba(25, 155, 254, 0.5)', 
            },
            emphasis: {
              focus: 'series'
            },
            data: [220, 102, 191, 234, 290, 130, 310]
          },
        ]
      })
    }
  }
}
</script>
